import { Component, ElementRef, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';
import { HttpModule } from '@angular/http';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
import { RouterModule, Router, ActivatedRoute } from '@angular/router';

import { Service } from '../../../server/server';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { HintsService } from '../../../public/serve/hints/hints.service';
declare var $: any;

@Component({
  selector: 'app-effect-add',
  templateUrl: './effect-add.component.html',
})
export class EffectAddComponent implements OnInit {

  constructor(
    private http: HttpClient, 
    private router: Router, 
    private server: Service, 
    private cudTableService: CudTableService,
    private hintsService:HintsService
  ) { }

  ngOnInit() {
    this.createUserId = this.server.getuser();
    //console.log(this.createUserId)
    this.superiorAdd = "最上级";
  }

  @Output()
  addOutput: EventEmitter<any> = new EventEmitter();

  @Input() superiorAdd: any;

  @Input() superiorAddId: any;

  public createUserId: any;

  //datatype
  adddate: addDatatype = {
    createUserId: this.createUserId,
    functionName: '',
    functionCode: '',
    functionEName: '',
    description: '',
    enable: true,
    functionType: true,
    pageUrl: '',
    functionId: '',
    parentId: ''
  };

  //清空
  @ViewChild('newlyAddedForm') formElement: HTMLFormElement;
  reset() {
    $("#add").modal("hide");
    this.formElement.reset();
    setTimeout(() => {
      this.adddate.enable = true;
      this.adddate.functionType = true;
    }, 300);
  }

  submitAdd() {
    this.adddate.createUserId = this.createUserId;
    this.adddate.parentId = this.superiorAddId;
    this.adddate.enable = Number(this.adddate.enable);
    this.adddate.functionType = Number(this.adddate.functionType);
    let url = this.server.baseUrl+'user/functions/add';
    let data: addDatatype = this.adddate;
    //console.log(data)
    this.cudTableService.postRequest(url, data).then((res: any) => {
      this.hintsService.messages(res.message,res.result_code);
      if (res.result_code == '0000') {
        this.reset();
        //prompt
        this.addOutput.emit(res.result_code);
        //update
      }
    }).catch()
  }

}
export class addDatatype {
  createUserId: string;
  description: string;
  enable: any;
  functionCode: string;
  functionEName: string;
  functionName: string;
  functionType: any;
  pageUrl: string;
  functionId?: string
  parentId?: string;
}

